package core

import (
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"strings"
)

func GetAppRoot() string {
	_, filename, _, ok := runtime.Caller(1)
	if !ok {
		panic("get dir failure.")
	}
	return path.Join(path.Dir(path.Dir(path.Dir(path.Dir(filename)))), "")
}

func GetRunPath() string {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		panic(err)
	}
	path, err := filepath.Abs(file)
	if err != nil {
		panic(err)
	}
	if runtime.GOOS == "windows" {
		path = strings.Replace(path, "\\", "/", -1)
	}
	i := strings.LastIndex(path, "/")
	if i < 0 {
		panic(`Can't find "/" or "\".`)
	}
	return string(path[0:i])
}
